var app = getApp();
var WxParse = require('../../../wxParse/wxParse.js');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    src: "/yiliao/images/budong.mp3", //音频路径
    poster: "/wedding/images/poster.png", //音频封面图片
    zname: "你根本不懂", //音频路径音频名字
    author: "季彦霖", //音频作者名字
    hide: true, //留言模态显示隐藏
    pinglun: [],
    shishi: "",
    shishijin: 0,
    aa: 0,
    speed: 0,
    miao2: 0,
    play: true,
    toastHidden3: false,
    toastHidden4: false,
    input: false
  },


  wxParseTagATap: function(e) {
    console.log(e)
    let link = e.currentTarget.dataset.src
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/webview/webview?src=' + link,
    })
  },
  pause: function() {
    this.audio = wx.createAudioContext("myAudio");
    this.audio.pause();
    this.setData({
      play: false
    })
  },
  play: function() {
    this.audio = wx.createAudioContext("myAudio");
    this.audio.play();
    this.setData({
      play: true
    })
  },
  funtimeupdate: function(u) {
    var that = this;
    var ii = parseInt(u.detail.currentTime); //每秒时间
    var a = that.data.aa;
    var b = parseInt(that.data.shishijin);
    a++;
    if (a < 10) {
      a = "0" + a;
    }
    if (a > 59) {
      a = 0;
      b++;
    }
    if (b == 0 || b < 10) {
      b = "0" + b;
    }
    var ss = parseInt(u.detail.duration); //总时间
    var miao1 = parseInt(ss / 60); //总时间-分
    var miao2 = parseFloat(ss % 60); //总时间-秒
    var speed = parseInt(100);
    var aa = parseFloat(speed / ss);
    var dd = ii * aa;
    var time = setTimeout(function() {
      that.setData({
        shishi: miao1,
        shishijin: b,
        miao2: miao2,
        speed: dd,
        aa: a,
      })
    }, 1000)

  },
  /**
   * 留言模态显示
   */
  liuyan: function() {
    this.setData({
      hide: false,
      input: true
    })
  },
  /**
   * 留言模态隐藏
   */
  hide: function(e) {
    this.setData({
      hide: true
    })
  },
  show: function() {
    this.setData({
      hide: false
    })
  },
  /**
   * 提交留言
   */
  formSubmit: function(e) {
    var that = this;
    var arr = this.data.pinglun;
    var obj = {};
    var values = e.detail.value.liuyan;
    var m_comment = e.detail.value.liuyan;
    var kc_id = e.detail.value.kc_id;
    var openid = wx.getStorageSync('openid');
    if (e.detail.value.liuyan == "") {
      wx.showModal({
        title: '内容不能为空',
        content: '',
      })
    } else {
      var data = new Date(); //当前时间
      var year = data.getFullYear(); //当前时间的年
      var month = data.getMonth(); ////当前时间的月
      month++;
      var day = data.getDate(); //当前时间的日
      var time = year + "-" + month + "-" + day;
      var name = that.data.userInfo.nickName;
      let zan = that.data.zan
      console.log(zan)
      var img = that.data.userInfo.avatarUrl;
      obj.zan = zan;
      obj.m_img = that.data.userInfo.avatarUrl;
      obj.name = that.data.userInfo.nickName;
      obj.m_comment = e.detail.value.liuyan;
      obj.m_time = time;
      arr.push(obj);
      this.setData({
        hide: true,
        pinglun: arr
      })
      app.util.request({
        'url': "entry/wxapp/Roomcomentarray",
        data: {
          name: that.data.userInfo.nickName,
          m_img: that.data.userInfo.avatarUrl,
          m_comment: m_comment,
          kc_id: kc_id,
          m_openid: openid,
          m_type: 3
        },
        success: function(res) {
          that.setData({
            input: false
          })
        },
        fail: function(err) {
          console.log(err)
        },
      })

      //console.log(this.data.pinglun);
    }
  },
  /**
   * 详情点赞
   */

  zan: function(e) {
    var that = this;
    // 活动id
    var id = e.currentTarget.dataset.id
    console.log(e)
    var openid = wx.getStorageSync('openid');
    var zans = parseInt(this.data.zan);
    if (this.data.toastHidden3 == false) {
      zans += 1;
      app.util.request({
        'url': 'entry/wxapp/SaveCollect',
        headers: {
          'Content-Type': 'application/json',
        },
        data: {
          openid: openid,
          goods_id: id,
          cerated_type: 2
        },
        dataType: 'json',
        success: function(res) {
          //console.log(res);
          wx.showToast({
            title: '谢谢点赞',
            icon: 'success',
            duration: 1500,
          })
        }
      })
      this.setData({
        toastHidden3: true,
        zan: zans
      })
    } else {
      zans -= 1;
      console.log('给个赞呗')
      app.util.request({
        'url': 'entry/wxapp/SaveCollect',
        'cachetime': '0',
        data: {
          openid: openid,
          goods_id: id,
          cerated_type: 2
        },
        dataType: 'json',
        success: function(res) {
          // console.log(res);
          wx.showToast({
            title: '给个赞呗',
            icon: 'success',
            duration: 1500,
          })

        }
      })
      this.setData({
        toastHidden3: false,
        zan: zans
      })
    }
  },
  /**
   * 评论点赞
   */
  plzan: function(e) {
    var self = this;
    var arr = self.data.pinglun;
    var i = e.currentTarget.dataset.index;
    // arr[i].dianz++;
    // console.log(e)
    app.util.request({
      'url': 'entry/wxapp/Zengpldianz',
      data: {
        openid: wx.getStorageSync('openid'),
        m_id: e.currentTarget.dataset.id
      },
      success: function(res) {
        console.log(res)
        var num = res.data;
        if (num == 1) {
          arr[i].dianz--;
        } else {
          arr[i].dianz++;
        }
        self.setData({
          pinglun: arr
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var title = options.title
    var bgc = wx.getStorageSync('color')
    var userInfo = wx.getStorageSync('userInfo')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    var that = this
    var id = options.id
    var hz_id = options.hz_id
    var openid = wx.getStorageSync('openid')
    app.util.request({
      'url': 'entry/wxapp/Hzxq',
      data: {
        hz_id: hz_id
      },
      success: function(res) {
        console.log(res)
        that.setData({
          hzxq: res.data.data,
          mp3: res.data.data.hz_mp3
        })
        //console.log(that.data.hzxq.hz_count)

        WxParse.wxParse('articles', 'html', res.data.data.hz_count, that, 5);
      },
      fail: function(err) {
        console.log(err)
      },

    })

    app.util.request({
      'url': 'entry/wxapp/Allhzzan',
      headers: {
        'Content-Type': 'application/json',
      },
      data: {
        openid: openid,
        goods_id: hz_id
      },
      success: function(res) {
        //console.log(res)
        that.setData({
          zan: res.data.data
        })
      }
    })
    app.util.request({
      'url': 'entry/wxapp/Scurl',
      success: function(res) {

        that.setData({
          dataurl: res.data.data,
        })
        //console.log(res.data.data)
      },
      fail: function(err) {
        console.log(err)
      },
    })
    //患者评论详情
    app.util.request({
      'url': "entry/wxapp/Allcoments",
      data: {
        hz_id: hz_id
      },
      'cachetime': '0',
      success: function(res) {
        console.log(res)
        that.setData({
          pinglun: res.data.data
        })
        // WxParse.wxParse('article', 'html', res.data.data.z_content, that, 5);
      },
      fail: function(err) {
        console.log(err)
      },
    })

    wx.setNavigationBarTitle({
      title: title
    })
    that.setData({
      userInfo: userInfo
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    /* 音频自动播放 */
    this.audio = wx.createAudioContext("myAudio");
    this.audio.play();
  },
  dianji: function() {
    console.log(this.audio.seek);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: this.data.title
    }
  }
})